package com.bjfu.college.pojo;

import cn.hutool.core.lang.Singleton;
import com.bjfu.college.utils.ExcelUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright@:
 *
 * @Author: Pan Zifeng
 * @Date: 2023/07/20/15:25
 * @Description:
 */
public class GlobalSetting {

    private static GlobalSetting instance;

    private Map<String, List<String>> subjectMap;


    private GlobalSetting() {
        // 选科组合与要求Map
        subjectMap = new HashMap<>();
        // excel导入
        try {
            excelToMap(subjectMap, "选科组合与选科要求对应表（含组合）.xlsx");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static GlobalSetting getInstance() {

        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new GlobalSetting();
                }
            }
        }
        return instance;
    }

    public Map<String, List<String>> getSubjectMap() {
        return subjectMap;
    }

    private void excelToMap(Map<String,List<String>> subjectMap, String fileName) throws IOException {
        ClassPathResource classPathResource = new ClassPathResource(File.separator +  "static" +  File.separator + fileName);
        InputStream inputStream = classPathResource.getInputStream();

        List<Map<String,String>> list = ExcelUtils.excelToShopIdList(inputStream);

        System.out.println("当前excel行数:"+list.size());
        System.out.println("当前excel列数:"+list.get(0).keySet().size());

        if(list.get(0).get("col0").equals("选科组合") && list.get(0).get("col1").equals("选科要求")) {
            for (int i = 1; i < list.size(); i++) {
                String subject = list.get(i).get("col0");
                String subjectRequirement = list.get(i).get("col1");
                List<String> subjectRequirementList = subjectMap.getOrDefault(subject, new ArrayList<>());
                subjectRequirementList.add(subjectRequirement);
                subjectMap.put(subject, subjectRequirementList);
            }
        }
    }
}
